from Bio import SeqIO
import sys
import numpy as np
import pandas as pd


def longestCommonSubsequence(A, B):
    m, n = len(A), len(B)
    dp = [[0 for _ in range(n)] for _ in range(m)]
    seq = []
    for i in range(0, m):
        for j in range(0, n):
            if A[i] == B[j]:
                dp[i][j] = 1
                if dp[i-1][j-1] > 0 and i > 0 and j > 0:
                    dp[i][j] = dp[i-1][j-1] + 1
            else:
                dp[i][j] = 0

    dp = np.array(dp)
    dp_max = np.max(dp)
    # print(dp)
    for i, j in np.array(np.where(dp == dp_max)).T:
        # print(i-dp_max+1,i+1)
        seq.append(A[i-dp_max+1:i+1])
    # print(seq)
    return list(set(seq))


def dfs(text, seq):
    # print(text,seq)
    seqs = []
    for k in text:
        # print(k)
        seqs+=longestCommonSubsequence(k, seq)
    # print(seqs)
    return list(set(seqs))


data = [str(k.seq) for k in SeqIO.parse(sys.argv[1], "fasta")]
if len(data) == 1:
    text = data[0]
else:
    text = longestCommonSubsequence(data[0], data[1])
    for i in range(2, len(data)):
        text = dfs(text, data[i])
        if len(text) == 0:
            text = []
            break
text = sorted(text, reverse=True)
print(text)
print(text[0])
